﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDD4Fun.Factory;

namespace DDD4Fun.Strategy
{
    //与调用策略接口的交互类
    class FruitDestructor
    {
        private BaseFacroty _factory = null;

        public FruitDestructor(BaseFacroty factory)
        {
            _factory = factory;
        }

        IFruitCutStrategy _fruitCut;
        IFruitThrowStrategy _fruitThrow;

        public void Cut(IFruit fruit, IFruitCutStrategy fruitCut)
        {
            fruitCut.Cut(fruit);
        }

        public void Throw(IFruit fruit, IFruitThrowStrategy fruitThrow)
        {
            fruitThrow.Throw(fruit);
        }

        public void testThrowFruit()
        {
            foreach (KeyValuePair<string, object> kvp in _factory.package)
            {
                int selectindex = GenerateIndex();
                switch (selectindex)
                {
                    case 2:
                        _fruitThrow = new CurveThrow();
                        break;
                    case 1:
                        _fruitThrow = new SlashThrow();
                        break;
                    case 0:
                        _fruitThrow = new StraightThrow();
                        break;
                    default: break;
                }

                Throw(kvp.Value as IFruit, _fruitThrow);
               
            }

            foreach (KeyValuePair<string, object> kvp in _factory.package)
            {
                int selectindex = GenerateIndex();
                switch (selectindex)
                {
                    case 0:
                        _fruitCut = new CurveCut();
                        break;
                    case 1:
                        _fruitCut = new SlashCut();
                        break;
                    case 2:
                        _fruitCut = new StraightCut();
                        break;
                    default: break;
                }
                Cut(kvp.Value as IFruit, _fruitCut);
            }
        }

        private int GenerateIndex()
        {
            Random r = new Random();
            int randomnum = r.Next(0, 100);
            int selectindex = randomnum % 3;
            return selectindex;
        }
    }
}
